<?php

class mdl_account_basic extends ujn_model {

	function getAllCount() {
		$sql = "select count(1) c from `{$this->db_name}`.`{$this->tbl_name}`;";
		$c = $this->_db->getOne($sql);
		return isset($c['c']) ? (int)$c['c'] : 0;
	}

	function makeAccount($sitemid, $invite_uid = '') {
		$def = ujn::getCFG('max');
		$sql =<<<EOT
insert into `{$this->db_name}`.`{$this->tbl_name}`
set `sitemid` = '{$sitemid}', `created`=unix_timestamp(),
`pick`='{$def['pick']}', `wine`='{$def['wine']}',
`nickname`='', `icon`='', `invite_uid`='{$invite_uid}';
EOT;
		$this->_db->query($sql);
		return array(
			'uid' => $this->_db->insertID(),
			'pick' => $def['pick'],
			'wine' => $def['wine']
		);
	}

	function get($uid = '', $sitemid = '') {

		if(!empty($uid))		$cond = "`uid`='{$uid}'";
		if(!empty($sitemid))	$cond = "`sitemid`='{$sitemid}'";

		$sql =<<<EOT
select * from `{$this->db_name}`.`{$this->tbl_name}`
where {$cond} limit 1;
EOT;
		return $this->_db->getOne($sql);
	}

	function mod($uid, $nickname = '', $icon = '', $pick = '', $wine = '') {
		$sets = array();
		if(!empty($nickname)) $sets[] = "`nickname`='{$nickname}'";
		if(!empty($icon)) $sets[] = "`icon`='{$icon}'";
		if(!empty($pick)) $sets[] = "`pick`='{$pick}'";
		if(!empty($wine)) $sets[] = "`wine`='{$wine}'";
		if(empty($sets)) return false;

		$sets = implode(',', $sets);
		$sql =<<<EOT
update `{$this->db_name}`.`{$this->tbl_name}`
set {$sets} where `uid` = '{$uid}';
EOT;
		$this->_db->query($sql);
		return $this->_db->affectedRows();
	}

	protected function __construct() {
		parent::__construct(__CLASS__);
		$this->_db	= $this->mysql();
	}

    /**
     * @return mdl_account_basic
     */
	static function getInstance() {
		return parent::getInstance(__CLASS__);
	}
}